package org.example;

import com.github.javafaker.Faker;
import org.example.entity.TaskEntity;
import org.example.service.TaskHandler;
import org.example.service.impl.DefaultOutHandlerMethod;
import org.example.service.impl.TaskHandlerImpl;
import org.example.utils.IpUtils;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Hello world!
 *
 */
public class App 
{
    private static String RTSP_TO_RTMP = "ffmpeg -re -i rtsp://127.0.0.1:8554/test   -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:1935/live";
    private static String RTMP_TO_HLS = "ffmpeg -i rtmp://127.0.0.1:1935/live -vcodec copy -vprofile baseline -acodec copy -ar 1 -f flv rtmp://127.0.0.1/live/room";
    private static String HLS_TO_MP4 = "ffmpeg  -i rtsp://127.0.0.1:8557/test   -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:1935/live";
    private static String MP4_TO_HLS = "ffmpeg  -i rtsp://127.0.0.1:8557/test   -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:1935/live";
    public static void main( String[] args )
    {
        File file = new File("D：/ppio/sd");
        file.getName();
        boolean directory = file.isDirectory();
//       TaskHandler rtspToRtmp = new TaskHandlerImpl(new DefaultOutHandlerMethod());
//        TaskEntity rtsp = rtspToRtmp.process("rtsp", RTSP_TO_RTMP);
//
//        System.out.println("执行结束");

    }
    public Process rtspToRtmp(){
        System.out.println("我ye进来了");
        Runtime run =Runtime.getRuntime();
        Process p = null;
        try {
            p = run.exec(RTSP_TO_RTMP);
            InputStream ins= p.getInputStream();
            InputStream ers= p.getErrorStream();
            new Thread(new inputStreamThread(ins)).start();
            new Thread(new inputStreamThread(ers)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return p;
    }
    public  void rtspToRtmp1(){
        System.out.println("我进来了");
        Runtime run =Runtime.getRuntime();
        Process p = null;
        try {
            p = run.exec(RTSP_TO_RTMP);
            InputStream ers = p.getErrorStream();
            byte[] b = new byte[1000];
            int num = 0;
            while((num=ers.read(b))!=-1){
                System.out.println(new String(b,"gb2312"));
            }

            System.out.println("子线程没执行完");
            p.waitFor();
            System.out.println("结束了");
        }catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Process rtmpToHls(){
        System.out.println("我ye进来了");
        Runtime run =Runtime.getRuntime();
        Process p = null;
        try {
            p = run.exec(RTMP_TO_HLS);
            InputStream ins= p.getInputStream();
            InputStream ers= p.getErrorStream();
            new Thread(new inputStreamThread(ins)).start();
            new Thread(new inputStreamThread(ers)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return p;
    }

    public void HlsToMp4(){
        Runtime run =Runtime.getRuntime();
        try {
            Process p = run.exec(HLS_TO_MP4);
            InputStream ins= p.getInputStream();
            InputStream ers= p.getErrorStream();
//            new Thread(new inputStreamThread(ins)).start();
            new Thread(new inputStreamThread(ers)).start();

            p.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    class inputStreamThread implements Runnable{
        private InputStream ins = null;
        private BufferedReader bfr = null;
        public inputStreamThread(InputStream ins){
            this.ins = ins;
            this.bfr = new BufferedReader(new InputStreamReader(ins));
        }
        @Override
        public void run() {
            String line = null;
            byte[] b = new byte[100];
            int num = 0;
            try {
                while((num=ins.read(b))!=-1){
                    System.out.println(new String(b,"gb2312"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
